using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class TemplateControls : BasePage
{
    public override Acms.Bll.Role.Roles[] AllowedRoles
    {
        get { return new Acms.Bll.Role.Roles[] { Acms.Bll.Role.Roles.Developer }; }
    }


    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            btnAdd.Visible = true;
            btnEdit.Visible = false;
            btnSave.Visible = false;
            btnCancel.Visible = false;
        }

    }

    protected void dsControls_SelectData(object sender, Acms.DataControls.SelectEventArgs<Acms.Bll.ControlType> e)
    {
        e.SetData(Acms.Bll.ControlType.GetAllBySiteId(AcmsContext.SiteId));
    }

    protected void dsControls_UpdatingData(object sender, Acms.DataControls.UpdatingDataEventArgs<Acms.Bll.ControlType> e)
    {
        if (e.Action == Acms.DataControls.UpdateAction.Insert)
        {
            e.Item.SiteId = AcmsContext.SiteId;
        }
    }

    protected void btnAdd_Click(object sender, EventArgs e)
    {
        details.Visible = true;
        legend.InnerHtml = "Add New Content Control";
        dvTemplateControls.ChangeMode(DetailsViewMode.Insert);
        btnAdd.Visible = false;
        btnEdit.Visible = false;
        btnSave.Visible = true;
        btnCancel.Visible = true;
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        if (dvTemplateControls.CurrentMode == DetailsViewMode.Insert)
        {
            dvTemplateControls.InsertItem(true);
        }
        else if (dvTemplateControls.CurrentMode == DetailsViewMode.Edit)
        {
            dvTemplateControls.UpdateItem(true);
        }
    }

    protected void gvTemplateControls_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (dvTemplateControls.CurrentMode != DetailsViewMode.ReadOnly)
        {
            dvTemplateControls.ChangeMode(DetailsViewMode.ReadOnly);
        }
        dvTemplateControls.PageIndex = gvTemplateControls.SelectedIndex;
        btnAdd.Visible = true;
        btnEdit.Visible = true;
        btnSave.Visible = false;
        btnCancel.Visible = false;
        details.Visible = true;
        legend.InnerHtml = "Edit Content Control";
    }

    protected void dvTemplateControls_ItemUpdated(object sender, DetailsViewUpdatedEventArgs e)
    {
        if (e.Exception == null)
        {
            gvTemplateControls.DataBind();
            btnEdit.Visible = true;
            btnAdd.Visible = true;
            btnSave.Visible = false;
            btnCancel.Visible = false;
        }
    }

    protected void btnEdit_Click(object sender, EventArgs e)
    {
        dvTemplateControls.ChangeMode(DetailsViewMode.Edit);
        btnAdd.Visible = false;
        btnEdit.Visible = false;
        btnSave.Visible = true;
        btnCancel.Visible = true;
    }

    protected void btnCancel_Click(object sender, EventArgs e)
    {
        dvTemplateControls.ChangeMode(DetailsViewMode.ReadOnly);
        if (gvTemplateControls.SelectedIndex >= 0)
        {
            btnEdit.Visible = true;
            details.Visible = true;
            legend.InnerHtml = "Edit Content Control";
        }
        else
        {
            btnEdit.Visible = false;
            details.Visible = false;

        }
        btnAdd.Visible = true;
        btnSave.Visible = false;
        btnCancel.Visible = false;

    }

    protected void dvTemplateControls_ItemInserted(object sender, DetailsViewInsertedEventArgs e)
    {
        if (e.Exception == null)
        {
            btnEdit.Visible = true;
            btnEdit.Visible = true;
            btnSave.Visible = false;
            btnCancel.Visible = false;

            gvTemplateControls.DataBind();

            int id = (int)e.Values["Id"];
            foreach(GridViewRow item in gvTemplateControls.Rows)
            {
                if ((int)gvTemplateControls.DataKeys[item.RowIndex]["Id"] == id)
                {
                    gvTemplateControls.SelectedIndex = item.RowIndex;
                    dvTemplateControls.PageIndex = item.DataItemIndex;
                }
            }
        }
    }
}
